<?php

namespace App\Http\Controllers\Activity\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Resources\Activity\Merchant\ShopOrderActivityDetailResource;
use App\Models\Activity\ShopOrderActivityModel;
use App\Models\Activity\ShopOrderActivityShopJoinRelModel;
use App\Models\Shop\ShopModel;
use App\Service\Activity\ShopOrderActivityFeedbackService;
use App\Service\Activity\ShopOrderActivityService;
use App\Service\Kms\Auth\AuthService;
use App\Service\OpenShop\Feishu\KfosService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopOrderActivityController extends BaseController
{
    /**
     * 获取活动详情
     *
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function detail(Request $request): JsonResponse
    {
        $activityId = (int)$request->input('activityId', 0);
        $shopId     = (int)$request->header('shopId', 0);

        $detailInfo = ShopOrderActivityService::getActivityInfo($activityId, $shopId);

        if ($detailInfo['code']) {
            return $this->success($detailInfo['data']);
        } else {
            return $this->fail($detailInfo['code'], $detailInfo['msg']);
        }
    }


    /**
     * 参与活动
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public function apply(Request $request): JsonResponse
    {
        $activityId = (int)$request->input('activityId', 0);
        $shopId     = $request->header('shopId', 0);
        $status     = (int)$request->input('status', 1); // 1参与 0 取消

        // 门店信息
        $shopInfo = ShopModel::query()->where('id', $shopId)->first();

        // 判断门店督导是否是当前用户
        $shopCouncilorId = $shopInfo->councilorId;
        $operateUid = (int)optional(getRmsUserInfoByAuth())->id;

        if ($operateUid == $shopCouncilorId) {
            return $this->fail(1003, "督导无法参与活动");
        }


        // 是否能参与
        $isCanApply = KfosService::checkHasFirstDeliveryNotes($shopId);
        if (!$isCanApply) {
            return $this->fail(1001, "您还没有发货单，无法参与活动");
        }

        // 是否已经参与
        $appliedStatus = ShopOrderActivityService::getAppliedStatus($activityId, $shopId);

        if ($appliedStatus) {
            return $this->fail(1002, "您已经参与过该活动");
        }

        ShopOrderActivityShopJoinRelModel::query()->updateOrCreate([
            'shopId'              => $shopId,
            'shopOrderActivityId' => $activityId
        ], [
            'status' => $status,
        ]);

        return $this->success([]);
    }

    /**
     * 获取活动详情
     * @param Request $request
     * @return JsonResponse
     */
    public function dominatingScreen(Request $request): JsonResponse
    {
        // 已废弃

        $shopId = (int)$request->header('shopId', 0);

        $data = ShopOrderActivityService::getDominatingScreen($shopId);
        return $this->success($data);

    }
}
